import java.util.Stack;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-09-27
 * Time: 21:55
 */
public class Main {
    public int evalRPN(String[] tokens) {
        Stack<String> stack=new Stack();
        for(int i=0;i<tokens.length;i++){
            //如果是运算符 取出两个数运算并将结果压入
            if(tokens[i].equals("+")||tokens[i].equals("-")||
                    tokens[i].equals("*")||tokens[i].equals("/")){

                //注意先取出的数放运算符右边
                int num2=Integer.valueOf(stack.pop());
                int num1=Integer.valueOf(stack.pop());
                int result=0;
                switch(tokens[i]){
                    case "+":
                        result=num1+num2;
                        break;
                    case "-":
                        result=num1-num2;
                        break;
                    case "*":
                        result=num1*num2;
                        break;
                    case "/":
                        result=num1/num2;
                        break;
                }
                stack.push(String.valueOf(result));
            }else{
                //是数字直接压入
                stack.push(tokens[i]);
            }
        }
        //栈中最后留下的数就是结果
        return Integer.valueOf(stack.pop());
    }
}
